package com.bigknow.appstore.web.base.listener;

import com.bigknow.appstore.auth.domain.shared.BeanProvider;
import com.bigknow.appstore.web.base.service.IConsoleService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;

/**
 * Created by TwelveFoldEr on 2016/5/11.
 */
public class ExtContextLoaderListener extends ContextLoaderListener {
    @Override
    public void contextInitialized(ServletContextEvent event) {
        ApplicationContext context = this.initWebApplicationContext(event.getServletContext());
        IConsoleService consoleService = (IConsoleService) context.getBean("consoleService");

        if (!consoleService.hasInit()) {
            consoleService.initConsole();
        }else{
            consoleService.updateConsole();
        }

        //OAuth初始化context
        WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
        BeanProvider.initialize(applicationContext);
    }
}
